package com.kma.summer2012.client.presenters.manager;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.kma.summer2012.client.presenters.Presenter;
import com.kma.summer2012.client.services.AbstractCallBack;
import com.kma.summer2012.client.services.catalogOfWorks.CrumbsCatalogOfWorksService;
import com.kma.summer2012.shared.model.UICrumbsWorkItem;
import com.smartgwt.client.types.TreeModelType;
import com.smartgwt.client.widgets.events.DrawEvent;
import com.smartgwt.client.widgets.events.DrawHandler;
import com.smartgwt.client.widgets.tree.Tree;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeGridField;
import com.smartgwt.client.widgets.tree.TreeNode;

import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: Администратор
 * Date: 14.02.13
 * Time: 19:12
 * To change this template use File | Settings | File Templates.
 */
public class WorksCatalogPresenter implements Presenter {
    public interface Display {
        TreeGrid getTreeGrid();

        Widget asWidget();
    }

    public Display getDisplay() {
        return display;
    }

    private final Display display;

    public WorksCatalogPresenter(Display display) {
        this.display = display;
        CrumbsCatalogOfWorksService.App.getInstance().getAllWorks(new AbstractCallBack<Set<UICrumbsWorkItem>>() {
            @Override
            public void onSuccess(Set<UICrumbsWorkItem> result) {
                TreeGridField workField = new TreeGridField("Work");

                TreeGridField workDescriptionField = new TreeGridField("WorkDescription");
                TreeGridField runTimeField = new TreeGridField("RunTime");
                WorksCatalogPresenter.this.display.getTreeGrid().setFields(workField, workDescriptionField, runTimeField);

                final Tree tree = new Tree();
                tree.setModelType(TreeModelType.PARENT);
                tree.setNameProperty("Work");
                tree.setIdField("Id");
                tree.setParentIdField("ReportsTo");
                tree.setShowRoot(false);

                TreeNode[] treeNodes = new TreeNode[result.size()];
                int i = 0;
                for (UICrumbsWorkItem item : result) {
                    int parentId;
                    if (item.isParent())
                        if (item.getParentId() == 0) {
                            parentId = 0;
                        } else parentId = item.getParentId();
                    else parentId = item.getParentId();
                    int runtime;
                    if (item.getRuntime() != null) {
                        runtime = item.getRuntime();
                        treeNodes[i] = new TreeGridNode(item.getId(),
                                parentId,
                                item.getWorkName(),
                                item.getWorkDescription(),
                                runtime,
                                item.isParent()
                        );
                    } else {
                        treeNodes[i] = new TreeGridNode(item.getId(),
                                parentId,
                                item.getWorkName(),
                                item.getWorkDescription(),
                                "",
                                item.isParent()
                        );
                        final String bUrl = GWT.getModuleBaseURL() + "/sc/skins/Enterprise/images/FileBrowser/folder.png";
                        treeNodes[i].setIcon(bUrl);
                    }
                    i++;
                }

                tree.setData(treeNodes);

                WorksCatalogPresenter.this.display.getTreeGrid().addDrawHandler(new DrawHandler() {
                    public void onDraw(DrawEvent event) {
                        tree.openAll();
                    }
                });

                WorksCatalogPresenter.this.display.getTreeGrid().setData(tree);
                WorksCatalogPresenter.this.display.getTreeGrid().sort();
            }
        });

    }

    private static class TreeGridNode extends TreeNode {

        public TreeGridNode(int id, int reportsTo, String name, String description, int runTime, boolean isParent) {
            setId(id);
            setReportsTo(reportsTo);
            setName(name);
            setDescription(description);
            setRunTime(runTime);
            setIsParent(isParent);


        }

        public TreeGridNode(int id, int reportsTo, String name, String description, String runTime, boolean isParent) {
            setId(id);
            setReportsTo(reportsTo);
            setName(name);
            setDescription(description);
            setRunTime(runTime);
            setIsParent(isParent);
            this.setAttribute("style", "color:red");
        }


        public void setId(int value) {
            setAttribute("Id", value);
        }

        public void setDescription(String value) {
            setAttribute("WorkDescription", value);
        }

        public void setReportsTo(int value) {
            setAttribute("ReportsTo", value);
        }

        public void setName(String name) {
            setAttribute("Work", name);
        }

        public void setRunTime(int runTime) {
            setAttribute("RunTime", runTime);
        }

        public void setRunTime(String runTime) {
            setAttribute("RunTime", runTime);
        }

        public void setIsParent(boolean isParent) {
            setAttribute("IsParent", isParent);
        }
    }

    @Override
    public void go(HasWidgets container) {
        container.clear();
        container.add(display.asWidget());
    }
}
